package edu.ccut.saturn.studio.dialog;

import java.util.List;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.logic.model.Connection;
import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnSwichNode;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnSwichVO;

public class SaturnRouterConnectionDialog extends TitleAreaDialog {
	
	private Connection connectionShape;
	private Shape source;
	private Shape target;
	private List<SaturnSwichVO> list = null;
	private Button buttonYes;
	private Button buttonNo;
	private Text textName;
	private SaturnSwichVO saturn;
	private Text textKey;
	
	public SaturnRouterConnectionDialog(Shell parentShell , Connection connectionShape) {
		super(parentShell);
		this.connectionShape = connectionShape;
		this.target = this.connectionShape.getTarget();
		this.source = this.connectionShape.getSource();
		this.list = ((SaturnSwichNode)this.source).getSaturnSwichConnections();
	}
	
	protected Control createDialogArea(Composite parent) {
		
		Composite area = (Composite) super.createDialogArea(parent);
		final Composite composite = new Composite(area, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);
		final GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, false, false);
		gd_composite.heightHint = 129;
		gd_composite.widthHint = 400;
		composite.setLayoutData(gd_composite);

		final Label label = new Label(composite, SWT.NONE);
		final GridData gd_label = new GridData();
		gd_label.verticalIndent = 5;
		label.setLayoutData(gd_label);
		label.setText(Messages.getString("SaturnRouterConnectionDialog.name")); //$NON-NLS-1$

		textName = new Text(composite, SWT.BORDER);
		final GridData gd_textName = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textName.verticalIndent = 5;
		textName.setLayoutData(gd_textName);

		final Label label_1 = new Label(composite, SWT.NONE);
		final GridData gd_label_1 = new GridData();
		gd_label_1.verticalIndent = 5;
		label_1.setLayoutData(gd_label_1);
		label_1.setText(Messages.getString("SaturnRouterConnectionDialog.expression")); //$NON-NLS-1$

		textKey = new Text(composite, SWT.BORDER);
		final GridData gd_textKey = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textKey.verticalIndent = 5;
		textKey.setLayoutData(gd_textKey);

		final Label label_2 = new Label(composite, SWT.NONE);
		final GridData gd_label_2 = new GridData();
		gd_label_2.verticalIndent = 5;
		label_2.setLayoutData(gd_label_2);
		label_2.setText(Messages.getString("SaturnRouterConnectionDialog.default")); //$NON-NLS-1$

		final Composite composite_1 = new Composite(composite, SWT.NONE);
		final GridData gd_composite_1 = new GridData(SWT.FILL, SWT.FILL, false, false);
		gd_composite_1.verticalIndent = 5;
		gd_composite_1.heightHint = 30;
		gd_composite_1.widthHint = 341;
		composite_1.setLayoutData(gd_composite_1);
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 2;
		composite_1.setLayout(gridLayout_1);

		buttonYes = new Button(composite_1, SWT.RADIO);
		buttonYes.setText(Messages.getString("SaturnRouterConnectionDialog.yes")); //$NON-NLS-1$

		buttonNo = new Button(composite_1, SWT.RADIO);
		final GridData gd_buttonNo = new GridData();
		gd_buttonNo.horizontalIndent = 20;
		buttonNo.setLayoutData(gd_buttonNo);
		buttonNo.setText(Messages.getString("SaturnRouterConnectionDialog.no")); //$NON-NLS-1$
		setMessage(Messages.getString("SaturnRouterConnectionDialog.AttributeConfig")); //$NON-NLS-1$
		
		saturn = getSaturnSwichConnectionsBySaturnSwichVO(this.list);
		if(saturn!=null){
			textKey.setText(saturn.getKey());
			iniButton(saturn.getIsDefault());
			
		}
		textName.setText(this.connectionShape.getLineName());
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		return area;
	}
	
	private void iniButton(String arg){
		if("false".equals(arg)){
			this.buttonNo.setSelection(true);
		}else if("true".equals(arg)){
			this.buttonYes.setSelection(true);
		}
		if(isEnableButtonYes(this.list)){
			this.buttonYes.setEnabled(false);
		}
	}
	
	private boolean isEnableButtonYes(List<SaturnSwichVO> list){
		for(SaturnSwichVO saturn : list){
			if(saturn.getIsDefault().equals("true")){
				return true;
			}
		}
		return false;
	}
	
	private SaturnSwichVO getSaturnSwichConnectionsBySaturnSwichVO(List<SaturnSwichVO> list){
		for(SaturnSwichVO saturn : list){
			if(saturn.getConnection().equals(this.target.getId())){
				return saturn;
			}
		}
		return null;
	}
	
	private String getButtonValue(){
		if(this.buttonYes.getSelection()){
			return "true";
		}else{
			return "false";
		}
	}
	
	protected Point getInitialSize() {
		return new Point(455, 245);
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}
	
	protected void buttonPressed(int buttonId) {
		if(buttonId==IDialogConstants.OK_ID){
			this.saturn.setKey(this.textKey.getText().trim());
			this.saturn.setConnection(this.target.getId());
			this.saturn.setIsDefault(getButtonValue());
			this.connectionShape.setLineName(textName.getText().trim());
			((SaturnSwichNode)this.source).setSaturnSwichConnections(this.list);
		}
		super.buttonPressed(buttonId);
	}
}